package com.tjp.eight.ui.fragment.modysec;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ResultModel;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.utils.TimeDown;
import com.tjp.eight.utils.ToatUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/7.
 */
public class FragmentMody extends BaseFragment<ModyPresenter> implements ModyView {
    @Bind(R.id.et_log_pass)
    EditText et_log_pass;
    @Bind(R.id.ed_phone_code)
    EditText ed_phone_code;
    @Bind(R.id.tv_btn_login)
    TextView tv_btn_login;
    @Bind(R.id.tv_get_code)
    TextView tv_get_code;

    private TimeDown timeDown;

    @Override
    protected ModyPresenter createPresenter() {
        return new ModyPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_mody, container, false);
        }
        ButterKnife.bind(this, contentView);
        initViews();
        return contentView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        if (timeDown != null) {
            timeDown.cancel();
        }
    }

    private void initViews() {
        et_log_pass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(" ")) {
                    String[] str = s.toString().split(" ");
                    String str1 = "";
                    for (int i = 0; i < str.length; i++) {
                        str1 += str[i];
                    }
                    et_log_pass.setText(str1);

                    et_log_pass.setSelection(start);

                }
                changLogTv();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        ed_phone_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(" ")) {
                    String[] str = s.toString().split(" ");
                    String str1 = "";
                    for (int i = 0; i < str.length; i++) {
                        str1 += str[i];
                    }
                    et_log_pass.setText(str1);

                    et_log_pass.setSelection(start);

                }
                changLogTv();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void changLogTv() {
        if (et_log_pass.getText().toString().length() >= 6 && ed_phone_code.getText().toString().length() == 6) {
            tv_btn_login.setEnabled(true);
        } else {
            tv_btn_login.setEnabled(false);
        }
    }

    @OnClick(R.id.tv_btn_login)
    void submit() {
        String code = ed_phone_code.getText().toString();
        String newp = et_log_pass.getText().toString();
        presenter.modifypass(getMJson(code, newp));
    }

    @OnClick(R.id.tv_get_code)
    void getCode() {
        presenter.modifypassgetcode(getSJson());
        timeDown();
    }

    private void timeDown() {
        tv_get_code.setEnabled(false);
        if (timeDown == null)
            timeDown = new TimeDown(60, new TimeDown.TimeDownListener() {
                @Override
                public void onPrepareCountDown() {

                }

                @Override
                public void onRefreshTime(int second) {
                    tv_get_code.setText(second + "秒");
                }

                @Override
                public void onFinishCountDown() {
                    tv_get_code.setText("重新获取");
                    tv_get_code.setEnabled(true);
                    timeDown = null;
                }
            });
        timeDown.start();
    }

    private String getMJson(String o, String n) {
        ModyPPModel model = new ModyPPModel();

        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        model.mobilecode = o;
        model.pwd = n;
        return new Gson().toJson(model);
    }

    private String getSJson() {
        ModyPPModel model = new ModyPPModel();

        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();

        return new Gson().toJson(model);
    }

    @Override
    public void modySuccess(ResultModel model) {
        if (model != null) {
            if (!StringUtils.isNullOrEmpty(model.msg))
                ToatUtils.toastSuccess(getContext(), model.msg);
        }
    }

    @Override
    public void modyPSuccess(ResultPWModel model) {
        if (model != null) {
            if (!StringUtils.isNullOrEmpty(model.msg))
                ToatUtils.toastSuccess(getContext(), model.msg);
            if (IsSuccessUtils.isSuccess(model.result)) {
                ToatUtils.toastSuccess(getContext(), "修改成功");
                SharePreferencesUtils.setUid(model.uid);
                SharePreferencesUtils.setLoginKey(model.loginkey);
            }
        }
    }
}
